Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implicit const parameters #10357

Merged
merged 3 commits into from
Aug 16, 2016
Merged

Implicit const parameters #10357

merged 3 commits into from
Aug 16, 2016

Conversation

ahejlsberg
Copy link
Member

With this PR a parameter is implicitly considered const when there are no assignments to the parameter anywhere in the declaring function (or functions nested within it). Similar to const variables, when a parameter is considered const, type guards for the parameter remain in effect in nested function expressions and arrow functions:

function f1(x: Object) {
    if (typeof x === "string") {
        setTimeout(() => console.log(x.charAt(0)), 0);
    }
}

function f2(info?: { name: string }) {
    if (!info) {
        return;
    }
    setTimeout(() => console.log(info.name), 0);
}

Above, x and info are implicitly const because there are no assignments to them anywhere in their containing functions, and therefore the type guards remain in effect in the callback functions.

Fixes #10180. Also see #7719 and a number of similar issues.

@@ -2157,6 +2157,7 @@ namespace ts {
/* @internal */ exportSymbol?: Symbol; // Exported symbol associated with this symbol
/* @internal */ constEnumOnlyModule?: boolean; // True if module contains only const enums or other modules with only const enums
/* @internal */ isReferenced?: boolean; // True if the symbol is referenced elsewhere
/* @internal */ isAssigned?: boolean; // True if the symbol has assignments
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Clarify that this flag is only set for parameters

@RyanCavanaugh
Copy link
Member

👍 . Seems like Travis isn't going anywhere...

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants